package net.hcpme.fragment;

import java.util.ArrayList;
import java.util.List;

import net.hcpme.activity.R;
import net.hcpme.adapter.TacticsAdapter;
import net.hcpme.dao.TacticsContentDataDao;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class TacticsFragment extends Fragment {

    public static final String TACTICS_MESSAGE_RECEIVE_BROADCAST = "tactics_message_receive_broadcast";

    private View mView;
    private TextView sendNote;
    private ListView tacticsLv;
    private TacticsAdapter tacticsAdapter;
    private List<TacticsContentDataDao> tacContentDaoList;

    private List<String> message = new ArrayList<String>();


    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(TACTICS_MESSAGE_RECEIVE_BROADCAST)) {
                Log.d("tag~~~", ">>>-------" + intent.getStringExtra("data"));
                message.add(intent.getStringExtra("data"));
                //需要重新组装消息
                tacticsAdapter.notifyDataSetChanged();
            }
        }
    };

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(TACTICS_MESSAGE_RECEIVE_BROADCAST);
        // 注册广播
        getActivity().registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        registerBoradcastReceiver();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.tactics_fragment, container, false);
        sendNote = (TextView) mView.findViewById(R.id.tactics_send_note);
        tacticsLv = (ListView)mView.findViewById(R.id.tactics_room_lv);
    	tacContentDaoList = new ArrayList<TacticsContentDataDao>();//待填入数据
    	TacticsContentDataDao dataDao = new TacticsContentDataDao();
    	tacContentDaoList.add(dataDao);
    	tacContentDaoList.add(dataDao);
    	tacticsAdapter = new TacticsAdapter(tacContentDaoList,inflater);
    	tacticsLv.setAdapter(tacticsAdapter);
        return mView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(mBroadcastReceiver);
    }

}
